package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysFormconfig;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 表单配置Controller
 * 
 * @author sunwei
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/system/formconfig")
public class SysFormconfigController extends BaseController
{
    @Autowired
    private ISysFormconfigService sysFormconfigService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysPostService postService;
    /**
     * 查询表单配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFormconfig sysFormconfig)
    {
        startPage();
        List<SysFormconfig> list = sysFormconfigService.selectSysFormconfigList(sysFormconfig);
        return getDataTable(list);
    }

    /**
     * 导出表单配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:export')")
    @Log(title = "表单配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFormconfig sysFormconfig)
    {
        List<SysFormconfig> list = sysFormconfigService.selectSysFormconfigList(sysFormconfig);
        ExcelUtil<SysFormconfig> util = new ExcelUtil<SysFormconfig>(SysFormconfig.class);
        util.exportExcel(response, list, "表单配置数据");
    }

    /**
     * 获取表单配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysFormconfigService.selectSysFormconfigById(id));
    }
    /**
     * 查询表单配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:query')")
    @GetMapping("/getFormconfigByUser")
    public AjaxResult getFormconfigByUser(SysFormconfig sysFormconfig)
    {
        //根据用户获取组织结构
        SysUser curuser = userService.selectUserByUserName(sysFormconfig.getUserName());
        //根据组织结构获取部门信息
        SysDept department = deptService.selectDeptById(curuser.getDeptId());
        //根据部门信息获取表单配置
        sysFormconfig.setAdminCodeOther(department.getAdministrativeCode());
        if(department.getAdministrativeCode()!=null){
            if(department.getAdministrativeCode().length()>=8 && sysFormconfig.getAdminLevel().equals("form_type_street")){
                sysFormconfig.setAdminCodeOther(department.getAdministrativeCode().substring(0,8));
            }
            if(department.getAdministrativeCode().length()>=10 && sysFormconfig.getAdminLevel().equals("form_type_community")){
                sysFormconfig.setAdminCodeOther(department.getAdministrativeCode().substring(0,10));
            }
            if(department.getAdministrativeCode().length()>=10 && sysFormconfig.getAdminLevel().equals("form_type_residential")){
                sysFormconfig.setAdminCodeOther(department.getAdministrativeCode().substring(0,10));
            }
        }
        List<SysFormconfig> config = sysFormconfigService.selectSysFormconfigList(sysFormconfig);
        if(config.size()>0){
            return AjaxResult.success(config.get(0));
        }else{
            return AjaxResult.success("未找到表单配置");
        }
    }
    /**
     * 查询表单配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:query')")
    @GetMapping("/getCityKpi")
    public AjaxResult getCityKpi(SysFormconfig sysFormconfig)
    {
      List<Map<String, Object>> kpis = sysFormconfigService.getCityKpi(sysFormconfig);
        if(kpis.size()>0){
            return AjaxResult.success(kpis);
        }else{
            return AjaxResult.success("未找到表单配置");
        }
    }


    /**
     * 新增表单配置
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:add')")
    @Log(title = "表单配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFormconfig sysFormconfig)
    {
        return toAjax(sysFormconfigService.insertSysFormconfig(sysFormconfig));
    }

    /**
     * 修改表单配置
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:edit')")
    @Log(title = "表单配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFormconfig sysFormconfig)
    {
        return toAjax(sysFormconfigService.updateSysFormconfig(sysFormconfig));
    }

    /**
     * 删除表单配置
     */
    @PreAuthorize("@ss.hasPermi('system:formconfig:remove')")
    @Log(title = "表单配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysFormconfigService.deleteSysFormconfigByIds(ids));
    }
}
